package com.slz3.business.module.goodsManagement.gooBaseGoodsSpecification.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.goodsManagement.gooBaseGoodsSpecification.domain.GooBaseGoodsSpecification;
import com.slz3.business.module.goodsManagement.gooBaseGoodsSpecification.domain.GooBaseGoodsSpecificationFactory;
import com.slz3.business.module.goodsManagement.gooBaseGoodsSpecification.steward.GooBaseGoodsSpecificationSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class GooBaseGoodsSpecificationServiceImpl implements GooBaseGoodsSpecificationService {
    @Override
    public GooBaseGoodsSpecification save(GooBaseGoodsSpecification gooBaseGoodsSpecification) {
        return Repositorys.gooBaseGoodsSpecification.save(GooBaseGoodsSpecificationFactory.initBaseFields(gooBaseGoodsSpecification));
    }
    @Override
    public Page<GooBaseGoodsSpecification> page(MultiValueMap<String, String> params) {
        Page<GooBaseGoodsSpecification> gooBaseGoodsSpecificationPage = Repositorys.gooBaseGoodsSpecification.findAll(
                GooBaseGoodsSpecificationSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return gooBaseGoodsSpecificationPage;
    }


    @Override
    public GooBaseGoodsSpecification modify(GooBaseGoodsSpecification gooBaseGoodsSpecificationNew) {
        return Repositorys.gooBaseGoodsSpecification.saveAndFlush(GooBaseGoodsSpecificationFactory.initBaseFields(gooBaseGoodsSpecificationNew));
    }

    @Override
    public Optional<GooBaseGoodsSpecification> single(UUID uuid) {
        return Repositorys.gooBaseGoodsSpecification.findById(uuid);
    }

    @Override
    public List<GooBaseGoodsSpecification> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.gooBaseGoodsSpecification.findAllById(uuids);
    }

    @Override
    public List<GooBaseGoodsSpecification> findByFkGooBaseGoodsUuid(UUID fkGooBaseGoodsUuid) {
        return Repositorys.gooBaseGoodsSpecification.findByFkGooBaseGoodsUuid(fkGooBaseGoodsUuid);
    }

    @Override
    public List<GooBaseGoodsSpecification> delete(List<GooBaseGoodsSpecification> gooBaseGoodsSpecificationList) {
        gooBaseGoodsSpecificationList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            GooBaseGoodsSpecificationFactory.initBaseFields(item);
        });
        return Services.gooBaseGoodsSpecificationTransactional.batchModify(gooBaseGoodsSpecificationList);
    }

}
